import { RoleInfoDTO } from '@shared/service-proxy/service-proxies';
import { Component, OnInit, ViewChild, Injector } from '@angular/core';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import { Validators, FormBuilder } from '@angular/forms';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ChangePermissionModalComponent } from './change-permission-modal/change-permission-modal.component';
import { SetRoleRemarkModalComponent } from './set-role-remark-modal/set-role-remark-modal.component';
@Component({
  selector: 'app-role-management',
  templateUrl: './role-management.component.html',
  styleUrls: ['./role-management.component.less']
})
export class RoleManagementComponent extends AppComponentBase implements OnInit {

  @ViewChild('changePermissionModal') changePermissionModal: ChangePermissionModalComponent;
  permissions = [];
  dataSet = [];
  loading = true;
  isVisible = false;
  isOkLoading = false;
  title: string;
  accreditShow = false;
  constructor(
    injector: Injector,
    private fb: FormBuilder,
    private _service: ServiceProxy) {
    super(injector);
  }

  role: RoleInfoDTO = new RoleInfoDTO();
  validateForm;
  load(): void {
    this.loading = true;
    this._service.getAllRoleInfosUsingGET().subscribe(result => {
      this.dataSet = result.result.fieldList;
      this.loading = false;
    })
  }


  ngOnInit() {
    this.validateForm = this.fb.group({
      name: [null, [Validators.required,Validators.maxLength(255)]],
    });
    this.load();
  }
  showModal(roleId?: string): void {
    this.role = new RoleInfoDTO();
    if (roleId) {
      this.title = this.l("edit");
      this._service.getRoleUsingGET(roleId).subscribe(result => {
        this.role = result.result;
      })
    }
    else {
      this.validateForm.reset();
      this.title = this.l("create");
    }
    this.isVisible = true;
  }

  submitForm(): void {

    this.isOkLoading = true;
    for (const i in this.validateForm.controls) {
      this.validateForm.controls[i].markAsDirty();
      this.validateForm.controls[i].updateValueAndValidity();
    }

    if (this.validateForm.valid) { // 验证通过
      let str = this.role.name.trim();

      this.role.name = str;
      if (this.role.id) {
        this._service.updateRoleUsingPUT(this.role, this.role.id).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.load();
            this.isVisible = false;
            this.message.success(this.l("save_success"));
          }
          else {
            this.message.info(this.l(result.error.message));
          }

        }, error => {
          this.isOkLoading = false;
          this.message.error(this.l(error.message));
        })
      }
      else {
        this._service.createRoleUsingPOST(this.role).subscribe(result => {
          this.isOkLoading = false;
          if (result.success) {
            this.load();
            this.isVisible = false;
            this.message.success(this.l("save_success"));
          }
          else
            this.message.info(this.l(result.error.message));
        }, error => {
          this.isOkLoading = false;
          this.message.error(this.l(error.message));
        })
      }

    } else { // 验证未通过
      this.isOkLoading = false;
      this.message.error(this.l('verify_failed_please_check_the_form_item'));
    }

  }
  handleOk(): void {
    this.submitForm();
  }

  handleCancel(): void {
    this.isVisible = false;
  }

  setPermissions(roleId?: string): void {
    this.changePermissionModal.show(roleId);
  }
}
